/*
 * GaugePanel.cpp
 * 
 *  Created on: Dec 28, 2010
 *  Author: Kevin Mueller
 */

#include "GaugePanel.h"

#include "../logic/Main.h"
#include "../logic/Settings.h"
#include "../logic/Clock.h"
#include "../logic/PositioningSystem.h"
#include "../logic/RoutingService.h"

#include <QtGui>

TimeGauge::TimeGauge(QWidget *parent) :
	QLabel(parent) {
	Main &map = Main::getInstance();
connect(&map.getClock(), SIGNAL(changed(Clock *)), this, SLOT(setTime(Clock *)));
}

void TimeGauge::setTime(Clock *clk) {
	setText(clk->getTime());
}

VelocityGauge::VelocityGauge(QWidget *parent) :
	QLabel(parent) {
	Main &map = Main::getInstance();
connect(&map.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo *)), this, SLOT(setSpeed(const ExtendedGeoPositionInfo *)));
}

void VelocityGauge::setSpeed(const ExtendedGeoPositionInfo *m) {
	AbstractSetting* s = Main::getInstance().getSettings().getUnit();
	setText(
			m != NULL ? QString::number(m->getVelocity(), 'f', 1).append(" ").append(
					s->getDescription(m->getUnitSystem()))
					: "0");
}

ArrivalGauge::ArrivalGauge(QWidget *parent) :
	QLabel(parent) {

	noArrival();

	const Main& main = Main::getInstance();
	connect(&main.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo*)), this, SLOT(setArrival(const ExtendedGeoPositionInfo*)));
	connect(&main.getRoutingService(), SIGNAL(startNavigation()), this, SLOT(setArrival()));
	connect(&main.getRoutingService(), SIGNAL(stopNavigation()), this, SLOT(noArrival()));
}

void ArrivalGauge::setArrival(const ExtendedGeoPositionInfo* gps) {
	const Main& main = Main::getInstance();
	int travel = main.getRoutingService().getRemainingTavelTime(gps);
	if (travel >= 0) {
		const Settings& settings = main.getSettings();
		const AbstractSetting* s = settings.getArrival();

		switch (s->getCurrent()) {
		case Arrival::ARRIVAL: {
			QDateTime current = QDateTime::currentDateTime();
			QDateTime arrival = current.addSecs(travel);
			setText(arrival.toString("hh:mm"));
		}
			break;
		case Arrival::DRIVING: {
			int hours = travel / 3600;
			int minutes = travel / 60 - hours * 60;
			setText(((minutes<10) ? QString("%1:0%2") : QString("%1:%2")).arg(hours).arg(minutes));
		}
			break;
		}
	} else {
		noArrival();
	}
}

void ArrivalGauge::setArrival() {
	setArrival(NULL);
}

void ArrivalGauge::noArrival() {
	setText("---");
}

DistanceGauge::DistanceGauge(QWidget *parent) :
	QLabel(parent) {

	noDistance();

	const Main& main = Main::getInstance();
	connect(&main.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo*)), this, SLOT(setDistance(const ExtendedGeoPositionInfo*)));
	connect(&main.getRoutingService(), SIGNAL(startNavigation()), this, SLOT(setDistance()));
	connect(&main.getRoutingService(), SIGNAL(stopNavigation()), this, SLOT(noDistance()));
}

void DistanceGauge::setDistance(const ExtendedGeoPositionInfo* gps) {
	const Main& main = Main::getInstance();

	float length = main.getRoutingService().getRemainingDistance(gps);
	if (length >= 0) {
		const Settings& settings = main.getSettings();
		const AbstractSetting* s = settings.getUnit();

		QString unit("km");
		if(s->getCurrent()==Unit::MH){
			length /= 1.6;
			unit = "M";
		}
		length /= 1000;

		setText(QString("%1 %2").arg(length).arg(unit));
	}
	else{
		noDistance();
	}
}

void DistanceGauge::setDistance(){
	setDistance(NULL);
}

void DistanceGauge::noDistance() {
	setText("---");
}


CurrentRoad::CurrentRoad(QWidget *parent) :
	QLabel(parent) {

	noName();

	const Main& main = Main::getInstance();
	connect(&main.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo*)), this, SLOT(setName(const ExtendedGeoPositionInfo*)));
	connect(&main.getRoutingService(), SIGNAL(stopNavigation()), this, SLOT(noName()));
}

void CurrentRoad::setName(const ExtendedGeoPositionInfo* info) {
	if(info != NULL){
		QString road = info->getRoadName();
		if(road != "")
			setText(road);
	}
}

void CurrentRoad::noName() {
	setText("---");
}



Compass::Compass(QWidget *parent) :
	QLabel(parent) {

	needle.load(IMAGEDIR "/needle.png");

	setMovingDirection(0);

	Main &main = Main::getInstance();
	connect(&main.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo *)), this, SLOT(setMovingDirection(const ExtendedGeoPositionInfo *)));
	connect(main.getSettings().getShowCompass(), SIGNAL(settingChanged(AbstractSetting *)), this, SLOT(setMovingDirection()));
}

void Compass::setMovingDirection(int degree) {
	QPixmap value;
	if(Main::getInstance().getSettings().getShowCompass()->getCurrent() == ShowCompass::ENABLED){
		QTransform myTransform;
		myTransform.rotate(-degree);
		value = needle.transformed(myTransform);
	}
	setPixmap(value);
	resize( sizeHint());
}

void Compass::setMovingDirection(const ExtendedGeoPositionInfo *m) {
	if (m != NULL) {
		if (Main::getInstance().isMoving()) {
			setMovingDirection(m->getMovingDirection());
			//setText(QString::number(m->getMovingDirection(), 'f', 1));
		} else {
			// TODO: start trigger mit offset, nach timeout wieder auf null stellen
		}
	} else {
		setMovingDirection(0);
	}
}

void Compass::setMovingDirection() {
	setMovingDirection(0);
}
